package com.firmail.filter;

import com.firmail.exception.type.ExceptionType;
import com.firmail.mock.util.GsonUtils;
import com.firmail.response.ResponseInfo;
import com.firmail.thread.MockThreadLocal;
import com.firmail.utils.ResponseExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@Component
@Slf4j
public class FacadeFilter implements Filter {

    private static final Set<String> ALLOWED_PATHS = Collections
            .unmodifiableSet(new HashSet<>(Arrays.asList("/authenticate",
                    "/authenticate/refreshtoken", "/authenticate/code", "/test/tigase/users",
                    "/code/guid", "/code/status", "/authenticate/ticket", "/key")));

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {

        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        MockThreadLocal.setThreadLocalRequest(request);
        MockThreadLocal.setThreadLocalResponse(response);

        //String version = request.getHeader(Constants.PARAM_VERSION);
        //String token = request.getHeader(Constants.PARAM_AUTHTOKEN);

        //log.debug("version->" + version + ", authtoken->" + token + ", platform->" + platform + ",device->" + device);

        String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
        // 判断请求路径是否需要token认证,版本以及设备
        /*boolean noAuth = ALLOWED_PATHS.contains(path);
        if (noAuth) {
            if (noAuth && ParamterUtils.isEmptyParam(version, platform, device)) {
                writerPrintln(response, ExceptionType.PARAMETER_ERROR_EXCEPTION);
                return;
            }
        } else {
            if (ParamterUtils.isEmptyParam(version, token, platform, device)) {
                writerPrintln(response, ExceptionType.PARAMETER_ERROR_EXCEPTION);
                return;
            }
            if (!TokenUtils.validateToken(token, platform)) {
                writerPrintln(response, ExceptionType.TOKEN_ERROR_EXCEPTION);
                return;
            }
        }
        Map<String, Object> threadLocalMap = MockThreadLocal.getThreadLocalMap();
        threadLocalMap.put(Constants.PARAM_VERSION, version);
        threadLocalMap.put(Constants.PARAM_AUTHTOKEN, token);
        threadLocalMap.put(Constants.PARAM_PLATFORM, platform);
        threadLocalMap.put(Constants.PARAM_DEVICE, device);
        threadLocalMap.put(Constants.REQUEST_ID, UUIDUtils.getUUID());*/

        chain.doFilter(servletRequest, servletResponse);
        //TokenUtils.validateToken(token, platform);
    }

    private void writerPrintln(HttpServletResponse response, ExceptionType type) throws IOException {
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            ResponseInfo<Object> info = new ResponseInfo<>();
            ResponseExceptionUtils.setException(info, type);
            writer.println(GsonUtils.toJson(info));
        } finally {
            writer.close();
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }

}